import os
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'

from google.appengine.dist import use_library
use_library('django', '1.2')

from google.appengine.ext.webapp import template


from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app

import random

counter = 0

'''
class HelloHandler(webapp.RequestHandler):
  def get(self):
    name = self.request.get('name')
    self.response.out.write('Hello ' + name)
    self.response.out.write("<a href='/static/SocialSpot.html'>My index.html</a>")
'''


class WrongAnswer:
  def __init__(self, question, guess, correct):
    self.question = question
    self.guess = guess
    self.correct = correct

class GradeQuizHandler(webapp.RequestHandler):
  def get(self):
    addition_guess = self.request.get('addition')
    capital_guess = self.request.get('capital')

    capital_guess = capital_guess.strip()
    capital_guess = capital_guess.lower()

    template_values = {'name' : self.request.get('name'), 'wrong_answers': []}

    if int(addition_guess) != 7:
        template_values['wrong_answers'].append(WrongAnswer("3 + 4", addition_guess, 7))
             
    if capital_guess != 'sacramento':
        template_values['wrong_answers'].append(WrongAnswer("the capital of California", capital_guess, "Sacramento"))        

    path = os.path.join(os.path.dirname(__file__), 'grade_quiz.html')
    self.response.out.write(template.render(path, template_values))

class QuizHandler(webapp.RequestHandler):
  def get(self):
    template_values = {'name' : self.request.get('name') }
    path = os.path.join(os.path.dirname(__file__), 'quiz.html')
    self.response.out.write(template.render(path, template_values))

class MainHandler(webapp.RequestHandler):
  def get(self):
    template_values = {}
    path = os.path.join(os.path.dirname(__file__), 'hello.html')
    self.response.out.write(template.render(path, template_values))

class DiceHandler(webapp.RequestHandler):
   def get(self):
     times = self.request.get('times')
     
     if(times.isdigit()==False) :
        self.response.out.write("YOU'RE AWFUL")
     
     else:     
         import random

         # Number of times to roll the dice.                                                                           
         counter = 0
         dice_1 = 0
         dice_2 = 0

         while counter < int(times):
            counter = counter + 1
           
            dice_1 = random.randint(1,6)
            dice_2 = random.randint(1, 6)
            self.response.out.write ("<br>Rolls: " + str(dice_1) + str(dice_2))
            if (dice_1 == 1):
                if (dice_2 == 1):
                    self.response.out.write ("<br>Got snake eyes!")
            if (dice_1 == 6):
                if (dice_2 == 6):
                    self.response.out.write ("<br>Got double sixes!")
     
     
         self.response.out.write('<br><br>Ran ' + times)

class GoodbyeHandler(webapp.RequestHandler):
  def get(self):
    self.response.out.write('Goodbye app engine world!')

def main():
  application = webapp.WSGIApplication([('/hello.*', MainHandler),
                                       ('/goodbye.*', GoodbyeHandler),
                                       ('/dice.*',DiceHandler),
                                       ('/quiz', QuizHandler),
                                       ('/grade_quiz', GradeQuizHandler)],
                                       debug=True)
  run_wsgi_app(application)

if __name__ == '__main__':
  main()
